#include <stdio.h>
#include <stdlib.h>
#include "aimage.h"
#include "aimage_utils.h"
#include "aimage_ppm.h"

int main( int argc, const char *argv[] )
{
	AIMAGE image;
	AIMAGE image2;
	int width, height;

	fprintf(stderr, "reading image\n");

	if ( argc != 1 && argc != 2 )
	{
		fprintf(stderr, "%s [in_file]\n", argv[0] );
		exit( 1 );
	}

	if ( argc == 2 )
	{
		AIMAGE_PPM_UTILS::load( image, argv[1] );
	}
	else
	{
		AIMAGE_PPM_UTILS::load( image, "stdin" );
	}

	fprintf(stderr, "copy image\n");
	image.get_dim( width, height );
	image2.alloc_image( width, height );
	AIU::cls( image2, FULL_INT, 0, 0 );
	image2.copy( image, 0, 0, width, height, 0, 0 );
	fprintf(stderr, "making mask\n");
	image2.makemask( .10 );
	//image2.makemask( .35 );

	//image.starfind();
	//image2.do_edge( image );

	int y;

	AFLOOD_DATA flood_data;

	flood_data.rs = FULL_INT; 
	flood_data.gs = FULL_INT; 
	flood_data.bs = FULL_INT;
	flood_data.rd = (rand() % (FULL_INT/2)) + FULL_INT/2; 
	flood_data.gd = (rand() % (FULL_INT/2)) + FULL_INT/2; 
	flood_data.bd = (rand() % (FULL_INT/2)) + FULL_INT/2; 
	flood_data.num_pixels = 0;

	for ( y = 0; y < height; ++y )
	{
		int x;
		if ( ( y % 100 ) == 0 )
		{
			fprintf(stderr,"Y = %d\n", y );
		}
		for ( x = 0; x < width; ++x )
		{
			image2.floodfill( x, y, flood_data );
		}
		if ( flood_data.num_pixels > 5 )
		{
			flood_data.rd = (rand() % (FULL_INT/2)) + FULL_INT/2; 
			flood_data.gd = (rand() % (FULL_INT/2)) + FULL_INT/2; 
			flood_data.bd = (rand() % (FULL_INT/2)) + FULL_INT/2;
			flood_data.num_pixels = 0; 
		}
	} 

	fprintf(stderr, "writing image\n");
	AIMAGE_PPM_UTILS::save( image2, "stdout", false );
}

